import { Controller, Post, Request, UseGuards, Body } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { AuthService } from './auth.service';
import { WechatService } from './wechat.service';

@Controller('auth')
export class AuthController {
  constructor(
    private readonly authService: AuthService,
    private readonly wechatService: WechatService,
  ) {}

  // 登录测试
  @UseGuards(AuthGuard('local'))
  @Post('/login')
  async login(@Request() req) {
    return this.authService.login(req.user);
  }

  @UseGuards(AuthGuard('local'))
  @Post('/register')
  async register(@Request() req) {
    return this.authService.register(req.user);
  }

  // 微信小程序登录
  // @UseGuards(AuthGuard('local'))
  @Post('/wechat/login')
  async wechatLogin(
    @Body()
    body: {
      code: string;
    },
  ) {
    if (!body.code) {
      return { code: 1, msg: 'code不能为空', data: '' };
    }
    return this.wechatService.wechatLogin(body);
  }
  // 微信小程序注册
  @Post('/wechat/register')
  async wechatRegister(
    @Body()
    body: {
      openid: string;
      sex: string;
      birthday: string;
      mbti: string;
      avatar: string;
      username: string;
    },
  ) {
    if (!body.openid) {
      return { openid: 1, msg: 'openid不能为空', data: '' };
    }
    return this.wechatService.registerWechatUser(body);
  }
}
